#include "mydialog.h"
#include "ui_mydialog.h"

MyDialog::MyDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::MyDialog)
{
    ui->setupUi(this);

    /*绑定所有按钮的信号到槽函数*/
    QList<QPushButton*> buttonList = this->findChildren<QPushButton*>();
    for(int i = 0;i < buttonList.size();i++)
    {
        connect(buttonList[i],SIGNAL(clicked(bool)),this,SLOT(pushbutton_cb()));
    }

    /*获取数据库实例*/
    myDB = MyDB::getInstance();
}

MyDialog::~MyDialog()
{
    delete ui;
}

void MyDialog::setFormat(int format)
{
    ui->stackedWidget->setCurrentIndex(format);
}

void MyDialog::pushbutton_cb()
{
    QObject* button = sender();

    if(button == ui->pushButton_cancel)
    {
        close();
    }else if(button == ui->pushButto_insertStudent)
    {
        myDB->addStudent(
            ui->lineEdit_studentID->text(),
            ui->lineEdit_studentName->text(),
            ui->lineEdit_studentGender->text(),
            ui->lineEdit_studentAge->text()
            );
        close();
    }else if(button == ui->pushButton_deleteStudent)
    {
        myDB->delStudent(ui->lineEdit_delStudentID->text());
        close();
    }else if(button == ui->pushButto_insertCourse)
    {
        myDB->addCourse(
            ui->lineEdit_courseID->text(),
            ui->lineEdit_courseName->text(),
            ui->lineEdit_courseCredit->text()
            );
        close();
    }else if(button == ui->pushButto_deleteCourse)
    {
        myDB->delCourse(ui->lineEdit_delCourseID->text());
        close();
    }else if(button == ui->lineEdit_insertGrade)
    {
        myDB->addGrade(
            ui->lineEdit_gradeSid->text(),
            ui->lineEdit_gradeCid->text(),
            ui->lineEdit_gradeGrade->text()
            );
        close();
    }else if(button == ui->lineEdit_deleteGrade)
    {
        myDB->delGrade(
            ui->lineEdit_delGradeSid->text(),
            ui->lineEdit_delGradeCid->text()
            );
        close();
    }

}
